import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

// import OnView from '../views/OneView.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',  //代表根路径
    redirect: "/login" //想让谁是首页 你就重定向到谁
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: {
      title: "首页"
    },
    children: [
      {
        path: "message",
        component: () => import('../views/MessageList.vue')
      },
      {
        path: "user",
        component: () => import('../views/UserView.vue')
      },
      {
        path: "add",
        component: () => import('../views/AddMessage.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue'),
    meta: {
      title: "登陆"
    }
  },
  {
    path: '/about',
    name: 'about',
    meta: {
      title: "关于"
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) =>{
  if(to.path == '/login'){
    return next();
  }
  if(localStorage.getItem('token')){
    next();
  }else{
    next('/login')
  }

})



export default router
